package  com.sat.common.utils

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.util.Log
import java.util.*

/**
 * Created by hayden on 2016-11-28.
 */
class ActivityManager private constructor() {
    /**
     * 保存所有Activity
     */
    private val activityStack by lazy {
        Stack<Activity>()
    }

    companion object {
        val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ActivityManager()
        }
    }


    /**
     * 退出Activity
     *
     * @param activity Activity
     */
    fun popActivity(activity: Activity?) {
        if (activity != null) {
            activityStack!!.remove(activity)
        }
    }

    /**
     * 获得当前栈顶的Activity
     *
     * @return Activity Activity
     */
    fun currentActivity(): Activity? {
        var activity: Activity? = null
        if (!activityStack!!.empty()) {
            activity = activityStack!!.lastElement()
        }
        return activity
    }

    fun <T> getActivity(clazz: Class<T>): Activity? {
        var activity: Activity? = null
        if (!activityStack!!.empty()) {
            val size = activityStack!!.size
            for (i in 0 until size) {
                val item = activityStack!![i]
                if (item != null && item.javaClass.name == clazz.name) {
                    activity = item
                    break
                }
            }
        }
        return activity
    }

    /**
     * 将当前Activity推入栈中
     *
     * @param activity Activity
     */
    fun pushActivity(activity: Activity) {
        activityStack!!.add(activity)
    }

    /**
     * 退出栈中其他所有Activity
     *
     * @param cls Class 类名
     */
    fun popOtherActivity(cls: Class<*>?) {
        if (null == cls) {
            return
        }
        for (activity in activityStack!!) {
            if (null == activity || activity.javaClass == cls) {
                continue
            }
            activity.finish()
        }
    }

    /**
     * 退出栈中所有Activity
     */
    fun popAllActivity() {
        while (true) {
            val activity = currentActivity() ?: break
            activity.finish()
            popActivity(activity)
        }
    }

    /**
     * 退出栈中所有Activity
     */
    fun popToActivity(clazz: Class<*>) {
        while (true) {
            val activity = currentActivity()
            if (activity == null || activity.javaClass == clazz) {
                break
            }
            activity.finish()
            popActivity(activity)
        }
    }

    /**
     * 查找Activity 栈中是否有某个Activity
     * 有的话返回该Activity s实例
     */
    fun firstActivity(): Activity? {
        var activity: Activity? = null
        try {
            activity = activityStack!!.elementAt(0)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return activity
    }

    /**
     * 结束指定类名的Activity
     */
    fun finishActivity(cls: Class<*>) {
        var i = 0
        val size = activityStack!!.size
        while (i < size) {
            if (activityStack!![i]!!.javaClass == cls) {
                finishActivity(activityStack!![i])
            }
            i++
        }
    }

    /**
     * 结束指定类名的Activity
     */
    fun finishActivityLastOne(cls: Class<*>) {
        val size = if (activityStack == null) 0 else activityStack!!.size - 1
        for (i in size downTo 0) {
            if (activityStack!![i]!!.javaClass == cls) {
                finishActivity(activityStack!![i])
                return
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity) {
        activity.finish()
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = activityStack!!.size
        while (i < size) {
            if (null != activityStack!![i]) {
                Log.e("finishActivity", activityStack!![i]!!.packageName)
                activityStack!![i]!!.finish()
            }
            i++
        }
        activityStack!!.clear()
    }

    /**
     * 退出应用程序
     */
    fun AppExit(context: Context) {
        try {
            finishAllActivity()
            val activityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            activityManager.restartPackage(context.packageName)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 获得当前栈顶的Activity
     *
     * @return Activity Activity
     */
    fun isCurrentActivity(cls: Class<*>?): Boolean {
        var isCurrent = false
        var activity: Activity? = null
        try {
            if (cls != null) {
                if (!activityStack!!.empty()) {
                    activity = activityStack!!.lastElement()
                    if (cls.simpleName == activity!!.javaClass.simpleName) {
                        isCurrent = true
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return isCurrent
    }

}